package com.itheima.test03;

import java.io.IOException;
import java.net.*;

/*
## 题目3（加强训练）
需求：编写程序，使用UDP协议的发送端给接收端发送一句话：“服务端你好啊！”；然后接收端给发送端返回一句话，内容随意。
 */
public class TestUdpClient {
    public static void main(String[] args) throws IOException, InterruptedException {
        //发送
        DatagramSocket socket = new DatagramSocket();
        String s = "我是客户端";
        InetAddress address = InetAddress.getByName("127.0.0.1");
        DatagramPacket packet = new DatagramPacket(s.getBytes(), s.getBytes().length, address, 11111);
        socket.send(packet);

        //接收
        socket = new DatagramSocket(22222);
        byte[] bytes = new byte[1024];
        DatagramPacket packet1 = new DatagramPacket(bytes, bytes.length);
        socket.receive(packet1);
        byte[] data = packet1.getData();
//        System.out.println(new String(data,0,data.length));
        System.out.println(new String(bytes,0,packet1.getLength()));

        socket.close();
    }
}
